import path from 'node:path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// 打包分析
// import { visualizer } from 'rollup-plugin-visualizer'
// 去除调试
import strip from '@rollup/plugin-strip'
import AutoImportTypes from 'auto-import-types'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import proxyData from './proxy.config'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode, ssrBuild }) => {
  const env = loadEnv(mode, process.cwd())

  return {
    base: env.VITE_PUBLIC_PATH,
    plugins: [
      vue({
        script: {
          defineModel: true
        }
      }),
      vueJsx(),
      // 正式环境打包查看各文件大小占比
      // visualizer({
      //   open: false,
      //   gzipSize: true,
      //   brotliSize: true
      // }),
      // 正式环境打包去除调试语句
      {
        ...strip({
          include: ['**/*.js', '**/*.vue', '**/*.ts', '**/*.jsx']
        }),
        apply: 'build'
      },
      AutoImportTypes(),
      AutoImport({
        dts: 'src/auto-imports.d.ts',
        imports: ['vue', 'pinia', 'vue/macros', '@vueuse/core'],
        exclude: ['createApp'],
        ignore: ['h'],
        eslintrc: {
          enabled: true
        },
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        extensions: ['vue'],
        dts: 'src/components.d.ts',
        resolvers: [ElementPlusResolver()]
      })
    ],
    resolve: {
      alias: {
        // 键必须以斜线开始和结束
        '@': path.resolve(__dirname, 'src')
      }
    },
    server: {
      // 配置调试服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
      host: '0.0.0.0',
      port: 80, // 服务器端口号
      open: false, // 是否自动打开浏览器
      cors: true, // 类型： boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
      proxy: proxyData
    }
  }
})
